﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Deanery.Controller.Directories;

namespace Deanery.Controller.PersonData
{
    class StreetController
    {
        private readonly deaneryEntities _db = new deaneryEntities();

        private string[] Divider(string str)
        {
            var split = str.Split('.');
            return split;
        }


        /// <summary>
        /// This method find reccord guid by name and guid of street type in table Streets
        /// </summary>
        /// <param name="name">string value to search reccord in table</param>
        ///  <returns>guid value of finded reccord</returns>
        public Guid FindGuidByStreetName(string name)
        {
            var spliter = Divider(name.Trim());

            var typeStreetObj = new TypeStreetController();
            
            var obj =
                (_db.Streets.Where(
                    t => t.nv_name_street == spliter[1] && t.id_type_street == typeStreetObj.FindGuidByName(spliter[0]))
                    .Select(t => t.id_street)).SingleOrDefault();
            return obj;
         }

        /// <summary>
        /// This method select all streets name from table Type_Street
        /// idea of this method: to return data from all conected tables with this one
        /// </summary>
        /// <returns>Array(every item is street name)</returns>
        public Array GetConectedData()
        {
            var list = from t in _db.Type_Street select t.nv_type_street_name;
            return list.ToArray();
        }
        
        /// <summary>
        /// This method performs INSERT one record to DB in table Street
        /// PROBLEM: method returns nothing
        /// </summary>
        /// <param name="reccord">ArrayList(one item - one cell value) with data to insert</param> 
        public void Insert(ArrayList reccord)
        {
            var typeStreetObj = new TypeStreetController();
            
            var obj = new Street
                {
                    id_street = Guid.NewGuid(),
                    id_type_street = typeStreetObj.FindGuidByName(reccord[0].ToString()),
                    nv_name_street = reccord[1].ToString()
                };
            if (2 < reccord.Count) obj.int_edbo = Convert.ToInt32(reccord[2]);

            _db.Streets.AddObject(obj);
            _db.SaveChanges();
        }

        /// <summary>
        /// This method for import
        /// performs INSERT one record to DB in table Street
        /// PROBLEM: method returns nothing
        /// </summary>
        /// <param name="name">string  value of street type and street (s.Ostafova)</param>
        public void Insert(string name)
        {
            var spliter = Divider(name.Trim());

            var typeStreetObj = new TypeStreetController();

            var obj = new Street
            {
                id_street = Guid.NewGuid(),
                id_type_street = typeStreetObj.FindGuidByName(spliter[0]),
                nv_name_street = spliter[1]
            };
        
            _db.Streets.AddObject(obj);
            _db.SaveChanges();
        }
    }
}
